package cn.fl.riskctrl.service.impl;

import cmm.comm.facade.framework.api.ISysCdMapApiService;
import cmm.comm.facade.framework.api.ISysCtrlCfgApiService;
import cmm.comm.facade.framework.condition.SysCdMapDQueryCondition;
import cmm.comm.facade.framework.dto.SysCdMapDDTO;
import cmm.comm.facade.upload.constant.SysUlFilesRecConstant;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.riskctrl.service.IRcCstBscInfoService;
import cn.fl.riskctrl.transform.RcCstBscInfoVOTransform;
import cn.fl.riskctrl.vo.RcCstBscInfoVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ser.Serializers;
import fl.constant.facade.framework.constant.FlowConstant;
import fl.constant.facade.framework.constant.SysCtrlCfgConstant;
import fl.constant.facade.framework.constant.SystemConstant;
import fl.riskctrl.facade.externalinterface.service.api.IExternalXjnpApplySearchApiService;
import fl.riskctrl.facade.riskctrl.api.*;
import fl.riskctrl.facade.riskctrl.condition.RcCstBscInfoQC;
import fl.riskctrl.facade.riskctrl.condition.RskCtrlIsCorrectQC;
import fl.riskctrl.facade.riskctrl.constant.EnumPrdTypCd;
import fl.riskctrl.facade.riskctrl.dto.DataResultDTO;
import fl.riskctrl.facade.riskctrl.dto.RcCstBscInfoDTO;
import fl.riskctrl.facade.riskctrl.dto.RcCstSocialRlDTO;
import fl.riskctrl.facade.riskctrl.dto.SplRiskDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 风险控制客户基础信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class RcCstBscInfoServiceImpl implements IRcCstBscInfoService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IRcCstBscInfoService.class);
	/**
	 * 代码映射表服务
	 */
	@Autowired
	private ISysCdMapApiService sysCdMapApiService;
	
	@Autowired
	private IRiskCtrlSearchApiService riskCtrlSearchApiService;

	@Autowired
	private IRiskCtrlModifyApiService riskCtrlModifyApiService;

	@Autowired
	private ISysOrgApiService sysOrgApiService;

	@Autowired
	private IRiskCtrlSyncApiService riskCtrlSyncApiService;

	/**城配风控API服务*/
	@Autowired
	public IRiskCtrlCpApiService riskCtrlCpApiService;

	/**重卡传统风控API服务*/
	@Autowired
	public IRiskCtrlZkApiService riskCtrlZkApiService;

	/**乘用车风控API服务*/
	@Autowired
	public IRiskCtrlApiService riskCtrlApiService;

	/**系统配置服务*/
	@Autowired
	public ISysCtrlCfgApiService sysCtrlCfgApiService;

	@Autowired
	private IRskLvlTempApiService rskLvlTempApiService;

	/**二手车征信前置服务*/
	@Autowired
	private IRiskCtrlEscApiService riskCtrlEscApiService;

    /**查询人行本地数据*/
	@Autowired
    private IExternalXjnpApplySearchApiService externalXjnpApplySearchApiService;

	@Autowired
	private IRiskCtrlKcApiService riskCtrlKcApiService;

	@Override
	public RcCstBscInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		RcCstBscInfoDTO dto = this.riskCtrlSearchApiService.getRcCstBscInfoById(id);//this.自定义ApiService.selectById(id);
		return RcCstBscInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(RcCstBscInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.rcCstBscInfoApiService.add(RcCstBscInfoVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.rcCstBscInfoApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
	@Override
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(RcCstBscInfoVO entity, String flag) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.riskCtrlModifyApiService.modifyCstResultInfo(RcCstBscInfoVOTransform.toDTO(entity), flag);
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		BaseLogUtils.newLogger("查询征信记录开始.page="+ JSONObject.toJSONString(page)).info();
		if(null == page){page = new Pager();}
		Pager pager = this.riskCtrlSearchApiService.searchRcCstBscInfoListPage(page);
		List<RcCstBscInfoDTO> dtos = (List<RcCstBscInfoDTO>)pager.getResultList();
		List<RcCstBscInfoVO> vos = this.setRcCstBscInfoOrg(RcCstBscInfoVOTransform.toVOList(dtos));
		if(CollectionUtils.isEmpty(dtos)){
            pager.setResultList(vos);
            return pager;
        }
        List<SplRiskDTO>  splRiskDTOList=this.setRcCstBscInfoSqlRiskList(dtos);
        for(RcCstBscInfoVO vo: vos){
            for (SplRiskDTO splRiskDTO:splRiskDTOList){
                if(vo.getId().longValue()==splRiskDTO.getId().longValue()){
                    if(splRiskDTO.getIsSplRisk()>0){
                        vo.setIsSplRisk(1);
                        vo.setSplRiskInfo("经销商15天内出现过C级客户，请谨慎操作");
                    }else if(splRiskDTO.getIsSplRisk()==0){
                        vo.setIsSplRisk(0);
                        vo.setSplRiskInfo("无");
                    }
                }
            }
        }
		pager.setResultList(vos);
		BaseLogUtils.newLogger("查询征信记录结束.page="+ JSONObject.toJSONString(pager)).info();
		return pager;
	}

    private  List<SplRiskDTO> setRcCstBscInfoSqlRiskList(List<RcCstBscInfoDTO> list){
		BaseLogUtils.newLogger("查询经销商风险信息开始。list="+JSONObject.toJSONString(list)).info();
        List<SplRiskDTO> splRiskDTOList=new ArrayList<SplRiskDTO>();
        RcCstBscInfoQC qc=new RcCstBscInfoQC();
        try {
            for (RcCstBscInfoDTO dto : list) {
                SplRiskDTO splRiskDTO = new SplRiskDTO();
                splRiskDTO.setId(dto.getId());
                splRiskDTO.setSplId(dto.getSplId());
                splRiskDTO.setCrtTm(dto.getCrtTm());
                splRiskDTO.setCtrmbefor15Day(DateUtils.getDateAddDate(dto.getCrtTm(),5, -15));
                splRiskDTOList.add(splRiskDTO);
            }
            qc.setSplRiskDTOList(splRiskDTOList);
            fl.riskctrl.facade.riskctrl.dto.DataResultDTO dataResultDTO = this.riskCtrlSearchApiService.searchRcCstBscInfoSqlRiskList(qc);
            if (dataResultDTO.getSuccess() != BaseConstant.IS_YESNO_YES) {
                splRiskDTOList=null;
                BaseLogUtils.error(logger, "selectById", dataResultDTO.getInfo());
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, dataResultDTO.getInfo());
            }
            splRiskDTOList = ( List<SplRiskDTO>) dataResultDTO.getData();


        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            BaseLogUtils.error(logger, "setRcCstBscInfoSqlRiskList", "查询供应商风险失败", e);
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, "查询供应商风险失败");
        }
        BaseLogUtils.newLogger("经销商风险查询结束splRiskDTOList="+JSON.toJSONString(splRiskDTOList)).info();
        return splRiskDTOList;
    }

	@Override
	public String exportRcCstBscInfo(RcCstBscInfoQC qc, String sysCd, String orgCd)throws BaseException {
    	if (qc == null) {
    		qc = new RcCstBscInfoQC();
		}
		String path = this.riskCtrlSearchApiService.exportRcCstBscInfo(qc, sysCd, orgCd);
		path = path.replace(PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_ROOT_PATH), PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_SERVER_PATH));
		return path;
	}

	@Override
	public String exportRcCstBscInfoDetail(RcCstBscInfoQC qc, String sysCd, String orgCd)throws BaseException {
		if (qc == null) {
			qc = new RcCstBscInfoQC();
		}
		String path = this.riskCtrlSearchApiService.exportRcCstBscInfoDetail(qc, sysCd, orgCd);
		path = path.replace(PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_ROOT_PATH), PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_SERVER_PATH));
		return path;
	}

	private List<RcCstBscInfoVO> setRcCstBscInfoOrg(List<RcCstBscInfoVO> vos) {
		List<SysOrgDTO> list = this.sysOrgApiService.searchSysOrgListBy(CurrentThreadContext.getCurrentSysCd());
		Map<String, SysOrgDTO> buOrgCdMap = new HashMap<>();
		if (CollectionUtils.isNotEmpty(list)) {
			for (SysOrgDTO sysOrgDTO : list) {
				buOrgCdMap.put(sysOrgDTO.getOrgCd(), sysOrgDTO);
			}
		}
		SysOrgDTO sysOrgDTO = null;
		String orgCd = null;
		if(CollectionUtils.isNotEmpty(vos)){
			for(RcCstBscInfoVO vo :vos){
				if (vo.getIsReaud() != null && vo.getIsReaud() == 1) {
					vo.setIsReaudNm("是");
				} else if (vo.getIsReaud() != null && vo.getIsReaud() == 0) {
					vo.setIsReaudNm("否");
				}
				if(vo.getMpMsgCd() != null && vo.getMpMsgCd().equals("0")){
					vo.setMpMsgCdNm("发送失败");
				}
				if(vo.getMpMsgCd() != null && vo.getMpMsgCd().equals("1")){
					vo.setMpMsgCdNm("发送成功");
				}
				if (vo.getIsValid() != null && vo.getIsValid() == 1) {
					vo.setIsValidNm("有效");
				} else if (vo.getIsValid() != null && vo.getIsValid() == 0) {
					vo.setIsValidNm("失效");
				}
				if (buOrgCdMap.containsKey(vo.getCstMgrOrgCd())) {
					sysOrgDTO = buOrgCdMap.get(vo.getCstMgrOrgCd());
					vo.setCstMgrOrgCdNm(sysOrgDTO.getOrgNm());
					orgCd = sysOrgDTO.getOrgCd();
					while(orgCd.length()>8){
						if("4".equals(sysOrgDTO.getOrgTypCd())){//分公司
							vo.setCyOrgCdNm(sysOrgDTO.getOrgNm());
						}else if("3".equals(sysOrgDTO.getOrgTypCd())){
							vo.setPrOrgCdNm(sysOrgDTO.getOrgNm());
							break;
						}
						orgCd = orgCd.substring(0,orgCd.length()-4);
						if(buOrgCdMap.containsKey(orgCd)){
							sysOrgDTO = buOrgCdMap.get(orgCd);
						}else{
							break;
						}
					}
				}
			}
		}
		return vos;
	}

	@Override
	public void syncRcCstBscInfoAudSts(List<Long> cstIdList, boolean allIsDl) throws BaseException {
		this.riskCtrlSyncApiService.refreshRcCstBscInfoAudSts(cstIdList, allIsDl);
	}

	@Override
	public boolean getRhAudSts() throws BaseException {
		this.rskLvlTempApiService.getRhAudSts();
		return true;
	}

	@Override
	public boolean getThdAudSts() throws BaseException {
		this.rskLvlTempApiService.getThdAudSts();
		return true;
	}

	/**
	 *  前置征信审核结果
	 */
	@Override
	public DataResultDTO getRiskCtrlIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		qc.setDataId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		return this.riskCtrlZkApiService.getRiskCtrlIsCorrect(qc, Boolean.valueOf(map.get(loadAllCd)));
	}

	@Override
	public DataResultDTO checkZZCIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		if ("2".equals(rcCstBscInfoDTO.getPrdTypCd())) {//城配业务
			return this.riskCtrlCpApiService.checkZZCIsCorrect(qc);
		} else if ("1".equals(rcCstBscInfoDTO.getPrdTypCd())) {//重卡传统业务
			return this.riskCtrlZkApiService.checkZZCIsCorrect(qc);
		} else if ("5".equals(rcCstBscInfoDTO.getPrdTypCd())) {//二手车
			return this.riskCtrlEscApiService.checkZZCIsCorrect(qc);
		} else {//乘用车业务
			return this.riskCtrlApiService.checkZZCIsCorrect(qc);
		}
	}

	/**
	 * 获取信加规则
	 */
	@Override
	public DataResultDTO checkXjIsCorrect(Long cstId) throws BaseException {
		DataResultDTO result = null;
		
		String msg = "";
		try {
			RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
			qc.setCstId(cstId);
			RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
			this.setRiskCtrl(rcCstBscInfoDTO, qc);
			
			if (EnumPrdTypCd.HEAVY_TRUCK.key().equals(rcCstBscInfoDTO.getPrdTypCd()) 
					|| EnumPrdTypCd.LIGHT_TRUCK.key().equals(rcCstBscInfoDTO.getPrdTypCd()) 
					|| EnumPrdTypCd.SECONDHANDCAR.key().equals(rcCstBscInfoDTO.getPrdTypCd())
					|| EnumPrdTypCd.CDD.key().equals(rcCstBscInfoDTO.getPrdTypCd())
					|| EnumPrdTypCd.CDD_UNDER_LINE.key().equals(rcCstBscInfoDTO.getPrdTypCd())
					|| EnumPrdTypCd.NEW_ENERGY.key().equals(rcCstBscInfoDTO.getPrdTypCd())) {
				// 重卡、轻卡、二手车、新能源、车抵贷（线上）、车抵贷（线下）
				result = this.riskCtrlZkApiService.checkXjIsCorrect(qc);
			}
			
			if ("4".equals(rcCstBscInfoDTO.getPrdTypCd())) {//客车
				result = this.riskCtrlKcApiService.checkXjIsCorrect(this.setRiskCtrl(rcCstBscInfoDTO, qc));
			}
		} catch (Exception e) {
			msg = "获取信加人行数据信息失败！";
			BaseLogUtils.error(logger, "checkXjIsCorrect", msg, e);
			throw new BaseException(msg);
		}
		
		return result;
	}
   /**
   *  获取信加本地规则
    * @methodName getXjLocalDataBaseIsCorrectData
    * @author: fuhaiyan@lionbridgecapital.cn
    * @createDate: 2019/5/28 20:37
    * @version: V1.0.0
    * @param cstId
    * @Return fl.riskctrl.facade.riskctrl.dto.DataResultDTO
    * @updateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
    */
    @Override
    public DataResultDTO getXjLocalDataBaseIsCorrectData(Long cstId) throws BaseException {
        DataResultDTO  result = new DataResultDTO(BaseConstant.IS_YESNO_NO);



        String msg = "";
        try {
            RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
            qc.setCstId(cstId);
            RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
            this.setRiskCtrl(rcCstBscInfoDTO, qc);

			cmm.mid.core.framework.dto.DataResultDTO dataResult = this.externalXjnpApplySearchApiService.checkXjIsCorrect(qc);
			result.setSuccess(dataResult.getSuccess());
			result.setId(dataResult.getId());
			result.setCode(dataResult.getCode());
			result.setInfo(dataResult.getInfo());
			result.setData(dataResult.getData());

        } catch (Exception e) {
            msg = "获取信加本地规则失败！";
            BaseLogUtils.error(logger, "getXjLocalDataBaseIsCorrectData", msg, e);
            throw new BaseException(msg);
        }

        return result;
    }


	@Override
	public DataResultDTO checkSynthMobileIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		qc.setDataId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		if ("2".equals(rcCstBscInfoDTO.getPrdTypCd())) {//城配业务
			return this.riskCtrlCpApiService.checkSynthMobileIsCorrect(qc);
		} else if ("1".equals(rcCstBscInfoDTO.getPrdTypCd())) {//重卡传统
			return this.riskCtrlZkApiService.checkSynthMobileIsCorrect(qc);
		} else if ("5".equals(rcCstBscInfoDTO.getPrdTypCd())) {//二手车
			return this.riskCtrlEscApiService.checkSynthMobileIsCorrect(qc);
		} else {//乘用车无三方综合手机核查接口
			return null;
		}
	}

	@Override
	public DataResultDTO checkSynthScoreIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		qc.setDataId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		
		if ("2".equals(rcCstBscInfoDTO.getPrdTypCd())) {//城配业务
			return this.riskCtrlCpApiService.checkSynthScoreIsCorrect(qc);
		} else if ("1".equals(rcCstBscInfoDTO.getPrdTypCd())) {//重卡传统
			return this.riskCtrlZkApiService.checkSynthScoreIsCorrect(qc);
		} else if ("5".equals(rcCstBscInfoDTO.getPrdTypCd())) {
			return this.riskCtrlEscApiService.checkSynthScoreIsCorrect(qc);
		} else if ("4".equals(rcCstBscInfoDTO.getPrdTypCd())) {
			// 客车
			return this.riskCtrlKcApiService.checkSynthScoreIsCorrect(this.setRiskCtrl(rcCstBscInfoDTO, qc));
		} else {//乘用车无三方综合评分接口
			return null;
		}
	}

    @Override
    public DataResultDTO getMPReportInfo(Long cstId,String reportType) throws BaseException {
        RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
        qc.setCstId(cstId);
        qc.setDataId(cstId);
        qc.setReportType(reportType);
        RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
        this.setRiskCtrl(rcCstBscInfoDTO, qc);
        return this.riskCtrlApiService.getReportInfo(qc);

    }
	// 专车·客车：百融案件
	@Override
	public DataResultDTO checkKcBadInfoIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		return this.riskCtrlKcApiService.checkBadIsCorrect(this.setRiskCtrl(new RcCstBscInfoDTO(), qc));
	}

	@Override
	public DataResultDTO checkPYJyIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		return this.riskCtrlApiService.checkPYJyIsCorrect(qc, Boolean.valueOf(map.get(loadAllCd)));
	}

	@Override
	public DataResultDTO checkPYJzIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		return this.riskCtrlApiService.checkPYJzIsCorrect(qc, null, Boolean.valueOf(map.get(loadAllCd)));
	}

	@Override
	public DataResultDTO checkKJYIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		return this.riskCtrlApiService.checkKJYIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkSYSIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		return this.riskCtrlApiService.checkSYSIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkPyEduIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		return this.riskCtrlApiService.checkPyEduIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkPyscrIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		return this.riskCtrlApiService.checkPYScoreIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkJXLIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		return this.riskCtrlApiService.checkJXLIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkBrIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		return this.riskCtrlApiService.checkBrIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkBrblIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		return this.riskCtrlApiService.checkBrblIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkBrPersonIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		return this.riskCtrlApiService.checkBrPersonIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkBrtelIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		return this.riskCtrlApiService.brTelcheckIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkTdtelIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		return this.riskCtrlApiService.tdTelcheckIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkTDIsCorrect(Long cstId) throws BaseException {
		RskCtrlIsCorrectQC qc = new RskCtrlIsCorrectQC();
		qc.setCstId(cstId);
		RcCstBscInfoDTO rcCstBscInfoDTO = new RcCstBscInfoDTO();
		this.setRiskCtrl(rcCstBscInfoDTO, qc);
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		return this.riskCtrlApiService.checkTDIsCorrect(qc);
	}

	@Override
	public RcCstBscInfoVO getCstBscInfoById(Long id) throws BaseException {
		//查询客户的关系数据以及附件信息, 并赋值
		String sysCd = CurrentThreadContext.getFlServiceSysCd();
		String orgCd = PropertiesCacheUtils.getProperty(SystemConstant.FL_ORG_CD);
		//String cfgCd = PropertiesCacheUtils.getProperty(DictionaryConstant.CODE_CST_BANK_REG_WAY_CD);
		RcCstBscInfoDTO dto = this.riskCtrlSearchApiService.getRcCstBscInfoDetailById(id, sysCd, orgCd, null);
		RcCstBscInfoVO vo = RcCstBscInfoVOTransform.toVO(dto);
		return vo;
	}

	/**获取开关*/
	private Map<String, String> getSwitch(String loadAllCd){
		String defOrgCd = PropertiesCacheUtils.getProperty(SystemConstant.FL_ORG_CD);
		//获取总开关, 如果关闭则按照正常情况返回
		List<String> ctrlCfgCodes = new ArrayList<>();
		ctrlCfgCodes.add(loadAllCd);
		Map<String, String> map = this.sysCtrlCfgApiService.searchSysCtrlCfgByCtrlCodes(ctrlCfgCodes, CurrentThreadContext.getFlServiceSysCd(), defOrgCd);
		//获取是否全部加载数据开关
		if(CollectionUtils.isMapEmpty(map)){
			map = new HashMap<>();
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, String> selectCstInfoLimitOne(String cstMgrBuOrgCd,String prjTypCd,String certNo) throws BaseException {
		//参数校验
		if(StringUtils.isEmpty(prjTypCd) || StringUtils.isEmpty(cstMgrBuOrgCd) || StringUtils.isEmpty(certNo)){
			BaseLogUtils.info(logger, "selectCstInfoLimitOne", "参数不能为空", CurrentThreadContext.getCurrentUserName());
			return new HashMap<>();
		}
		RcCstBscInfoQC qc = new RcCstBscInfoQC();
		//项目类型转换购车类型
		String prdTypCd = prjTypCdToPrdTypCd(prjTypCd);
		if(StringUtils.isEmpty(prdTypCd)){
			BaseLogUtils.info(logger, "selectCstInfoLimitOne", "项目类型转换购车类型为空", CurrentThreadContext.getCurrentUserName());
			return new HashMap<>();
		}
		Pager page = new Pager();
		qc.setPrdTypCd(prdTypCd);
		qc.setCertNo(certNo);
		qc.setCstMgrBuOrgCd(cstMgrBuOrgCd);
		qc.setIsReaud(0);
		qc.setOrderNo(1);
		qc.setIsValid(1);
		qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
		qc.setOrgCd(WebBaseConstant.SYS_P_ORG_CD);//TODO
		qc.setIsAuth(false);//不启用权限
		page.setCondition(qc);
		Pager pager = this.riskCtrlSearchApiService.searchRcCstBscInfoListPage(page);
		List<RcCstBscInfoDTO> dtos = (List<RcCstBscInfoDTO>)pager.getResultList();
		if(CollectionUtils.isEmpty(dtos)){
			BaseLogUtils.info(logger, "selectCstInfoLimitOne", "查询客户数据为空", CurrentThreadContext.getCurrentUserName());
			return new HashMap<String, String>();
		}
		List<RcCstBscInfoVO> vos = this.setRcCstBscInfoOrg(RcCstBscInfoVOTransform.toVOList(dtos));
		RcCstBscInfoVO rcCstBscInfoVO = vos.get(0);
		return resultMap(rcCstBscInfoVO);
	}
	/**
	 * 返回值Map
	 * @param vo
	 * @return
	 */
	private Map<String, String> resultMap(RcCstBscInfoVO vo) {
		Map<String, String> map  = new HashMap<>();
		//是否复议
		if(vo.getIsReaud() == 1){
			map.put("rskLvlCdNm", PropertiesCacheUtils.getProperty(FlowConstant.FLOW_CHECK_STATE_IN_STR));
			map.put("audStsDesc", "");
			return map;
		}
		//判断审核状态
		if(vo.getAudStsCd().equals(PropertiesCacheUtils.getProperty(FlowConstant.FLOW_CHECK_STATE_NON)) 
				|| vo.getAudStsCd().equals(PropertiesCacheUtils.getProperty(FlowConstant.FLOW_CHECK_STATE_IN))){//0、待提交  1、审核中
			map.put("rskLvlCdNm", vo.getAudStsCdNm());
			map.put("audStsDesc", "");
		}else{//展示用户级别
			//是否有效
			if(vo.getIsValid() == 1){//有效
				map.put("rskLvlCdNm", vo.getRskLvlCdNm());
			}else{
				map.put("rskLvlCdNm", vo.getRskLvlCdNm()+" (已过期)");
			}
			map.put("audStsDesc", vo.getAudStsDesc());
		}
		return map;
	}

	/*
	 * 项目类型转换购车类型
	 */
	private String prjTypCdToPrdTypCd(String prjTypCd) {
		BaseLogUtils.info(logger, "prjTypCdToPrdTypCd", "项目类型转换购车类型, 项目类型prjTypCd:["+prjTypCd+"]", CurrentThreadContext.getCurrentUserName());
		String prdTypCd = "";
		try {
			String mTypCd = PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.SysCdMapConstant.M_TYP_CD_PRD_TYP_CD);
			String sysCd = CurrentThreadContext.getFlServiceSysCd();
			SysCdMapDQueryCondition sysCdMapDQueryCondition = new SysCdMapDQueryCondition();
			sysCdMapDQueryCondition.setmTypCd(mTypCd);
			sysCdMapDQueryCondition.setSysCd(sysCd);
			sysCdMapDQueryCondition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
			List<SysCdMapDDTO> feeTypList = this.sysCdMapApiService.searchSysCdMapDWithTargetList(sysCdMapDQueryCondition);
			BaseLogUtils.info(logger, "prjTypCdToPrdTypCd", "项目类型转换购车类型, 获取配置列表:["+JSON.toJSONString(feeTypList)+"]", CurrentThreadContext.getCurrentUserName());
			
			if(CollectionUtils.isNotEmpty(feeTypList)){
				for (SysCdMapDDTO sysCdMapDDTO : feeTypList) {
					if(sysCdMapDDTO.getSCode().equals(prjTypCd)){
						prdTypCd = sysCdMapDDTO.getTCode();
					}
				}
			}
		} catch (Exception e) {
			BaseLogUtils.error(logger, "prjTypCdToPrdTypCd", "获取代码映射表失败." + e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return prdTypCd;
	}

	/**
	 * 设置承租人或担保人数据
	 */
	private RcCstBscInfoDTO setRiskCtrl(RcCstBscInfoDTO rcCstBscInfoDTO, RskCtrlIsCorrectQC qc) {
		RcCstBscInfoDTO rcDto = this.riskCtrlSearchApiService.getRcCstBscInfoById(qc.getCstId());
		if (rcDto != null) {//承租人
			qc.setSysCd(rcDto.getSysCd());//系统编码
			qc.setOrgCd(rcDto.getCstMgrOrgCd());//组织结构代码
			qc.setBuOrgCd(rcDto.getCstMgrBuOrgCd());//客户经理所属组织机构代码
			qc.setCertNo(rcDto.getCertNo());
			qc.setName(rcDto.getCstNm());
			qc.setPhone(rcDto.getMp1());
			qc.setMp(rcDto.getMp1());
		} else {//查询担保人或配偶
			RcCstSocialRlDTO dto = this.riskCtrlSearchApiService.searchRcCstSocialRl(qc.getCstId());
			if (null != dto) {
				qc.setCertNo(dto.getCertNo());
				qc.setName(dto.getRlNm());
				qc.setPhone(dto.getMp1());
				qc.setMp(dto.getMp1());
				rcDto = this.riskCtrlSearchApiService.getRcCstBscInfoById(dto.getCstId());//查询担保人活配偶对应的客户信息
				if (null != rcDto) {
					qc.setSysCd(rcDto.getSysCd());//系统编码
					qc.setOrgCd(rcDto.getCstMgrOrgCd());//组织结构代码
					qc.setBuOrgCd(rcDto.getCstMgrBuOrgCd());//客户经理所属组织机构代码
				}
			}
		}
		rcCstBscInfoDTO.setPrdTypCd(rcDto.getPrdTypCd());
		
		return rcDto;
	}

	@Override
	public Boolean batchGenerateAuthorization(List<String> cstId, String ip)
			throws BaseException {
		return true;
	}

	@Override
	public Boolean batchSendMassage(List<String> cstId) throws BaseException {
		return true;
	}
}
